#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "operations.h"

fraction_t *Multiply(fraction_t *a, fraction_t *b)
{
	fraction_t *result;
	long numerator;
	long denominator;

	if(0 != a->whole_number)
		PureFraction(a);
	if(0 != b->whole_number)
		PureFraction(b);
	numerator = a->numerator * b->numerator;
	denominator = a->denominator * b->denominator;
	result = MakeFraction(0, numerator, denominator);
	if(NULL != result)
	{
		ReduceFraction(result, 0);
		FixMix(result);
		return result;
	}
	return NULL;
}

fraction_t *Devide(fraction_t *a, fraction_t *b)
{
	fraction_t *result;
	long numerator;
	long denominator;

	if(0 != a->whole_number)
		PureFraction(a);
	if(0 != b->whole_number)
		PureFraction(b);
	numerator = a->numerator * b->denominator;
	denominator = a->denominator * b->numerator;
	result = MakeFraction(0, numerator, denominator);
	if(NULL != result)
	{
		ReduceFraction(result, 0);
		FixMix(result);
		return result;
	}
	return NULL;
}

fraction_t *Sum(fraction_t *a, fraction_t *b, int plus)
{
	fraction_t *result;
	long numerator;
	long denominator;

	if(0 != a->whole_number)
		PureFraction(a);
	if(0 != b->whole_number)
		PureFraction(b);
	if(plus)
		numerator = a->numerator * b->denominator + a->denominator * b->numerator;
	if(!plus)
		numerator = a->numerator * b->denominator - a->denominator * b->numerator;
	denominator = a->denominator * b->denominator;
	result = MakeFraction(0, numerator, denominator);
	if(NULL != result)
	{
		ReduceFraction(result, 0);
		FixMix(result);
		return result;
	}
	return NULL;
}

